package com.ssh.controller;

import com.ssh.common.Result;
import com.ssh.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/person")
public class PersonController {

    @Autowired
    PersonService personService;

    @ResponseBody
    @RequestMapping(value = "/persons")
    public Result persons() {
        return Result.success(personService.getPersons());
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Result adPerson() {
        Long i = personService.addPerson();
        if (i.intValue() < 0) {
            return Result.error("增加用户失败");
        }
        return Result.success();
    }

    @ResponseBody
    @RequestMapping(value = "/getPerson")
    public Result getPersonById(HttpServletRequest request, HttpServletResponse response, String pid) {
        if (pid == null){
            return Result.error("pid == null");
        }
        return Result.success(personService.getPersonById(Long.valueOf(pid)));
    }
}
